package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.CompoundButton;

import com.http.okhttp.HttpTask;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.VehicleActivityFindBinding;
import com.qbw.app.model.FindData;
import com.qbw.app.model.Vehicle;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.fragment.IKnowDialogFragment;
import com.qbw.app.module.common.fragment.LoopItemSelectTimeDialogFragment;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.request.impl.VehicleRequest;

/**
 * 寻车设置
 * @author xiansx
 * @date 2020-08-13
 */
public class VehicleFindSettingActivity extends CommonActivity<VehicleActivityFindBinding> implements LoopItemSelectTimeDialogFragment.Listener {

    private Vehicle mVehicle;
    private FindData mFindData;
    // todo 默认为“开”状态
    // todo 默认为2小时

    public static void startActivity(Context context, Vehicle mVehicle) {
        Intent intent = new Intent(context, VehicleFindSettingActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(Constant.KEY_VEHICLE, mVehicle);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_find;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mVehicle = (Vehicle)args.getSerializable(Constant.KEY_VEHICLE);
        mBinding.title.setSubTitle(mVehicle.getVehicleNick());
        mBinding.title.setSubTitleVisible(true);
        mBinding.title.setListener(this);
        udateUI();
        mBinding.executePendingBindings();
    }

    /**
     * 更新UI
     */
    private void udateUI() {
        if(mFindData != null) {
            mBinding.scSwitchFind.setOnCheckedChangeListener(null);
            mBinding.scSwitchFind.setChecked(mFindData.getLightSwitch() == 1 ? true : false);
            mBinding.tvFindDuration.setText(mFindData.getLightDuration() + "h");
        } else {
            mBinding.scSwitchFind.setOnCheckedChangeListener(null);
            mBinding.scSwitchFind.setChecked(false);
            mBinding.tvFindDuration.setText("1h");
        }

        mBinding.clFindDuration.setOnClickListener(this);
        mBinding.scSwitchFind.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(mFindData == null) {
                    mFindData = new FindData();
                }
                mFindData.setLightSwitch(isChecked ? 1 : 0);
                sendLightSwitchNm(mFindData.getLightSwitch());
                udateUI();
            }
        });
    }

    /**
     * 下发设置寻车开关
     */
    private void sendLightSwitchNm(int lightSwitch) {
        if(!TextUtils.isEmpty(mVehicle.getVehicleCode())) {
            mGeneralManager.getRequestManager()
                    .getVehicleRequest()
                    .sendLightSwitchNm(mVehicle.getVehicleCode(), lightSwitch,
                            this)
                    .addExtraParam("lightSwitch", lightSwitch)
                    .execute();
        }
    }

    /**
     * 设置寻车时长
     */
    private void sendLightDurationNm(int lightDuration) {
        if(!TextUtils.isEmpty(mVehicle.getVehicleCode())) {
            mGeneralManager.getRequestManager()
                    .getVehicleRequest()
                    .sendLightDurationNm(mVehicle.getVehicleCode(), lightDuration,
                            this)
                    .execute();
        }
    }

    /**
     * 查看寻车数据状态
     */
    private void getLightDataNm() {
        if(!TextUtils.isEmpty(mVehicle.getVehicleCode())) {
            mGeneralManager.getRequestManager()
                    .getVehicleRequest()
                    .getLightDataNm(mVehicle.getVehicleCode(),
                            this)
                    .execute();
        }
    }

    @Override
    public void onHttpStart(HttpTask httpTask) {

    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_LIGHT_DATA_V)) {
            FindData.Response response = (FindData.Response) entity;
            mFindData = response.getData();
            udateUI();
        } else if (method.equals(VehicleRequest.METHOD_LIGHT_SENDSWITCH_V)) {
            int lightSwitch = (int)httpTask.getExtraParam("lightSwitch");
            if(lightSwitch == 1) {
                UniversalToastUtil.showToast(R.string.nm_find_switch_on_success);
            } else if(lightSwitch == 0){
                UniversalToastUtil.showToast(R.string.nm_find_switch_off_success);
            } else {
                UniversalToastUtil.showToast(R.string.nm_find_setting_success);
            }
        } else if (method.equals(VehicleRequest.METHOD_LIGHT_SENDDURATION_V)) {
            UniversalToastUtil.showToast(R.string.nm_find_duration_success);
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_LIGHT_SENDSWITCH_V)) {
            UniversalToastUtil.showToast(R.string.nm_find_setting_fail);
        } else if (method.equals(VehicleRequest.METHOD_LIGHT_SENDDURATION_V)) {
            UniversalToastUtil.showToast(R.string.nm_find_duration_fail);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getLightDataNm();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(v.getId() == R.id.cl_find_duration) {
            int selectItem = 0;
            if(mFindData != null) {
                selectItem = mFindData.getLightDuration();
            }
            LoopItemSelectTimeDialogFragment.createInstanceForTimeSelect(selectItem)
                    .setListener(this)
                    .show(getSupportFragmentManager(), "time_select");
        }
    }

    @Override
    public void onRightAreaClick() {
        IKnowDialogFragment.createInstance(gs(R.string.nm_find_tips), Gravity.CENTER, "").show(getSupportFragmentManager(), "FindSettingTip");
    }

    @Override
    public void onSelectItem(String selectItem) {
        if(!TextUtils.isEmpty(selectItem)) {
            if(mFindData == null) {
                mFindData = new FindData();
            }
            mFindData.setLightDuration(Integer.valueOf(selectItem.replaceAll("h", "")));
            sendLightDurationNm(mFindData.getLightDuration());
            udateUI();
        }
    }

}
